package com.winit.generator.handler.impl;

import com.winit.generator.Constants;
import com.winit.generator.config.Configuration;
import com.winit.generator.handler.BaseHandler;
import com.winit.generator.model.EntityInfo;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;

public class EntityHandler extends BaseHandler<EntityInfo> {

    public EntityHandler(String ftlName, EntityInfo info) {
        this.ftlName = ftlName;
        this.info = info;
        this.savePath = Configuration.getString("base.baseDir") + File.separator
                + Configuration.getString("entity.path") + File.separator + info.getClassName()
                + Constants.FILE_SUFFIX_JAVA;

    }

    @Override
    public void combileParams(EntityInfo entityInfo) {
        this.param.put("packageStr", entityInfo.getEntityPackage());
        StringBuilder sb = new StringBuilder();
        for (String str : entityInfo.getImports()) {
            sb.append("import ").append(str).append(";\r\n");
        }
        this.param.put("importStr", sb.toString());
        this.param.put("entityDesc", entityInfo.getEntityDesc());
        this.param.put("className", entityInfo.getClassName());

        // 生成属性，getter,setter方法
        sb = new StringBuilder();
        Map<String, String> propRemarks = entityInfo.getPropRemarks();
        for (Entry<String, String> entry : entityInfo.getPropTypes().entrySet()) {
            String propName = entry.getKey();
            String propType = entry.getValue();

            if (propType == null) {
                propType = "Byte";
            }

            // 注释、类型、名称
            boolean isNotNeedAnnotation = propName.toLowerCase().contains("id")
                    || ("Byte".equals(propType) && !propName.startsWith("is"))
                    || propName.toLowerCase().contains("type")
                    || propName.toLowerCase().contains("stateno")
                    || propType.contains("状态")
                    || propType.contains("类型")
                    || "gmtCreate".equals(propName)
                    || "gmtModified".equals(propName)
                    || "isDeleted".equals(propName);

            if (isNotNeedAnnotation) {
                sb.append("    /**")
                        .append("\r\n     * ")
                        .append(propRemarks.get(propName))
                        .append("\r\n     */\r\n")
                        .append("    private ")
                        .append(propType)
                        .append(" ")
                        .append(propName)
                        .append(";\r\n");
            } else {
                sb.append("    /**")
                        .append("\r\n     * ")
                        .append(propRemarks.get(propName))
                        .append("\r\n     */")
                        .append("\r\n    @EntityFieldInfo(desc = \"")
                        .append(propRemarks.get(propName))
                        .append("\")")
                        .append("\r\n    private ")
                        .append(propType)
                        .append(" ")
                        .append(propName)
                        .append(";\r\n");
            }

        }

        this.param.put("propertiesStr", sb.toString());
    }
}
